package com.panopset.compat;

/**
 * A little something to allow all threads to cleanly shut down.
 *
 * (I just called it Zombie, instead of State, because I didn't want to conflict
 * with Thread.State, and plus it's 04:52 on a Saturday morning, so Zombie was
 * the first thing I thought of when thinking of something that could be active
 * or not.)
 *
 * @author Karl Dinwiddie
 *
 */
public final class Zombie {

    /**
     * Will be true for the life of the application. All threads should check
     * here to see if they should terminate (as well as their own terminating
     * logic if necessary).
     */
    private boolean active = true;

    /**
     * Stop application. There is no restart, kills all threads.
     */
    public static void stop() {
        getInstance().active = false;
    }

    /**
     * @return true if active.
     */
    public static boolean isActive() {
        return getInstance().active;
    }

    /**
     * Based on SingletonHolder inner class by Bill Pugh.
     *
     *
     * <h5>References</h5>
     * <ul>
     * <li>
     * <a href="http://en.wikipedia.org/wiki/Singleton_pattern">
     * http://en.wikipedia.org/wiki/Singleton_pattern </a></li>
     * </ul>
     *
     */
    private static final class Singleton {
        /**
         * Instance variable.
         */
        private static final Zombie INSTANCE = new Zombie();

        /**
         * Private constructor.
         */
        private Singleton() {
            // Prevent instantiation.
        }
    }

    /**
     * @return static Zombie instance.
     */
    private static Zombie getInstance() {
        return Singleton.INSTANCE;
    }

    /**
     * Private singleton constructor.
     */
    private Zombie() {
        // Prevent outside instantiation.
    }

}
